﻿
using Assets.Script.Human.Enemy;
using Assets.Script.Tools;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Assets.Script.HelpSystem.AtkSystem
{
    public class KnifeBullet : PlayerBulletBodyBase
    {
        private static GameObject effect;
        private Transform Enemy;

        Vector3 target;
        Quaternion targetRotation,myRotation;
        protected override void Update()
        {
            if (!isMoveSelf)
            {
                return;
            }
            if (Enemy!=null)
            {
                target = Enemy.position;
                target.y = transform.position.y;

                myRotation = transform.rotation;
                transform.LookAt(target);
                targetRotation = transform.rotation;

                transform.rotation = Quaternion.Lerp(myRotation, targetRotation, Time.deltaTime*2f);
            }
            else
            {
                FindEnemy();
            }
            transform.Translate(Vector3.forward * bulletData.BulletSpeed * Time.deltaTime * timeData.LocalTimeScale);
        }

        private void FindEnemy()
        {
            var cs = Physics.OverlapSphere(transform.position,30);
            foreach (var item in cs)
            {
                if (item.tag=="Enemy")
                {
                    Enemy = item.transform;
                    break;
                }
            }
        }

        protected override void DieEffect(bool hitEnemy)
        {
            if (hitEnemy)
            {
                return;
            }
            if (effect == null)
            {
                effect = Resources.Load<GameObject>(ResourcePath.GunBulletExplodeEffect);
            };
            var de = Instantiate(effect, transform.position, new Quaternion(), null);
            Destroy(de, 2f);
        }


    }



}
